<?php


namespace app\controller\equipment;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 终端故障
 */
class Malfunction extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取终端故障列表
     */
	public function api_equipment_malfunction_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'equipment_name' => isset($msg['data']['equipment_name'])?$msg['data']['equipment_name']:'',
            'linkman' => isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'equipment_status' => isset($msg['data']['equipment_status'])?$msg['data']['equipment_status']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['type'])){
            $where .= " AND type =".$info['type'];
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['equipment_status'])){
            $where .= " AND equipment_status =".$info['equipment_status'];
        }
        if(!empty($info['linkman'])){
            $where .= " AND linkman LIKE '%".$info['linkman']."%'";
        }
        if(!empty($info['linkman_mobile'])){
            $where .= " AND linkman_mobile LIKE '%".$info['linkman_mobile']."%'";
        }
        if(!empty($info['equipment_name'])){
            $where .= " AND equipment_name LIKE '%".$info['equipment_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['region_name'])){
            $where .= " AND region_name LIKE '%".$info['region_name']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
		$list = $db->query("select * from equipment_malfunction where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from equipment_malfunction WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}



    /**
     * 添加
     */
    public function api_equipment_malfunction_add($msg)
    {   
        $info = [
            'equipment_id' =>  isset($msg['data']['equipment_id'])?$msg['data']['equipment_id']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
            'malfunction_level' => isset($msg['data']['malfunction_level'])?$msg['data']['malfunction_level']:'',
            'malfunction_time' => isset($msg['data']['malfunction_time'])?$msg['data']['malfunction_time']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['equipment_pic'] = isset($msg['data']['equipment_pic'])?$msg['data']['equipment_pic']:'';
        $info['describe'] = isset($msg['data']['describe'])?$msg['data']['describe']:'';
        if(!is_numeric($info['type']) || $info['type'] > 4 || $info['type'] < 1){
            return $this->outMessage(-1,'请选择正确得故障类型');
        }
        if(!empty($info['describe'])){
            if(!$this->validate->checkName($info['describe'],2,100)){
                return $this->outMessage(-1,'请输入正确的描述');
            }
        }
        $equipment = $this->table->find('equipment_list',['id'=>$info['equipment_id']]);
        if(!$equipment){
            return $this->outMessage(-1,'该设备不存在');
        }
        $data = [
            'equipment_id' => $info['equipment_id'],
            'equipment_code' => $equipment['code'],
            'equipment_name' => $equipment['name'],
            'ip' => $equipment['ip'],
            'linkman' => $equipment['linkman'],
            'linkman_mobile' => $equipment['linkman_mobile'],
            'type' => $info['type'],
            'describe' => $info['describe'],
            'merchant_id' => $equipment['merchant_id'],
            'merchant_name' => $equipment['merchant_name'],
            'region_id' => $equipment['region_id'],
            'region_name' => $equipment['region_name'],
            'itoc_id' => $equipment['itoc_id'],
            'itoc_name' => $equipment['itoc_name'],
            'status' => 2,
            'malfunction_level' => $info['malfunction_level'],
            'equipment_pic' => $info['equipment_pic'],
            'malfunction_time' => $info['malfunction_time'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('equipment_malfunction',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }


 
    /**
     * 编辑
     */
    public function api_equipment_malfunction_edit($msg)
    {   
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'principal' => isset($msg['data']['principal'])?$msg['data']['principal']:'',
            'equipment_status' => isset($msg['data']['equipment_status'])?$msg['data']['equipment_status']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['remark'] = isset($msg['data']['remark'])?$msg['data']['remark']:'';
        if(!empty($info['remark'])){
            if(!$this->validate->checkName($info['remark'],2,100)){
                return $this->outMessage(-1,'请输入正确的描述');
            }
        }
        $data = [
            'principal' => $info['principal'],
            'equipment_status' => $info['equipment_status'],
            'remark' => $info['remark'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('equipment_malfunction',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }


}
